package springsecurity.config;

import lombok.Data;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Component;
import springsecurity.utlis.Statements;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: c.c
 * @Date: 2022/6/14
 * @Time: 11:23
 * @Description:  权限配置
 */
@Component
@Data
public class AuthConfig {

    /**
     * 不需要登录 就能访问
     */
    private final List<RequestMatcher> IGNORE_MATCHERS = new ArrayList<>();

    /**
     * 不需要认证的
     */
//    private final List<RequestMatcher> IGNORE_AUTH_MATCHERS = new ArrayList<>();

    /**
     * 认证后 才能访问
     */
    private final List<RequestMatcher> LOGIN_SHARE_MATCHERS = new ArrayList<>();


    //不加这个注解，就不会初始化
    @PostConstruct
    public void init(){
        this.convertToMatchers(IGNORE_MATCHERS, Statements.getIgnoreURL());
        this.convertToMatchers(LOGIN_SHARE_MATCHERS, Statements.getLoginShareUrls());
    }

    private void convertToMatchers(List<RequestMatcher> resultList,List<String> targetList){
        targetList.forEach(path -> resultList.add(new AntPathRequestMatcher(path)));
    }
}
